'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 联盟BOSS信息
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var moment = require('moment');
var mongoose = require('mongoose');
var dataApi = require('../libs/dataApi');
var constantUtil = require('../util/constantUtil.js');
var unionBossConf = require('../config/unionBossConf.js');

var _initBossFightTimes = function () {
    return constantUtil.value('union_boss_challenge_times');
};

var _initCurrBossId = function () {
    var first_boss_conf = unionBossConf.getFirstBossConf();
    return first_boss_conf.id;
};

var _initCurrBossHp = function () {
    var first_boss_conf = unionBossConf.getFirstBossConf();
    return first_boss_conf.hp;
};

/**
 * 伤害列表信息
 */
var damageInfoSchema = new mongoose.Schema({
    // 角色ID
    role_id: Number,
    // 伤害值
    damage: Number,
    // 最后修改时间
    last_modify_tm: Number,
});

/**
 * 已击杀BOSS信息
 */
var killedBossInfoSchema = new mongoose.Schema({
    // BOSS标识
    boss_id: Number,
    // 击杀者
    killer_id: Number,
    // 击杀者
    killer_name: String,
});

/**
 * 联盟BOSS信息
 */
var unionBossSchema = new mongoose.Schema({
    // 联盟id
    union_id: {
        type: Number,
        required: true,
        unique: true,
        index: true
    },

    // 伤害列表
    damage_list: {
        type: [damageInfoSchema],
        default: []
    },

    // 锁定玩家ID
    lock_role_id: {
        type: Number,
        default: 0
    },

    // 锁定玩家名称
    lock_role_name: {
        type: String,
        default: ''
    },

    // 锁定结束时间
    lock_end_tm: {
        type: Number,
        default: 0
    },

    // 当前BOSS_ID
    curr_boss_id: {
        type: Number,
        default: _initCurrBossId
    },

    // 当前BOSS血量
    curr_boss_hp: {
        type: Number,
        default: _initCurrBossHp
    },

    // 已击杀BOSS信息
    killed_boss_list: {
        type: [killedBossInfoSchema],
        default: []
    },
});

/**
 * 查询已击杀BOSS信息
 * @return {[type]} [description]
 */
unionBossSchema.methods.getKilledBossInfo = function (boss_id) {
    return _.find(this.killed_boss_list, {
        boss_id: boss_id
    });
};

/**
 * 保存BOSS伤害值
 * @return {[type]} [description]
 */
unionBossSchema.methods.setDamage = function (role_id, damage) {
    var self = this;
    if (damage < 1) {
        return false;
    }

    self.curr_boss_hp -= damage;
    if (self.curr_boss_hp < 0) {
        self.curr_boss_hp = 0;
    }

    var now_tm = moment().unix();
    var damage_info = _.find(self.damage_list, {
        role_id: role_id
    });
    if (!!damage_info) {
        damage_info.damage += damage;
        damage_info.last_modify_tm = now_tm;
    } else {
        damage_info = {
            role_id: role_id,
            damage: damage,
            last_modify_tm: now_tm
        };
        self.damage_list.push(damage_info);
    }

    return true;
};

/**
 * 保存击杀BOSS列表中
 * @return {[type]} [description]
 */
unionBossSchema.methods.appendToKilledBossList = function (role_id, role_name) {
    var self = this;
    if (self.curr_boss_hp > 0) {
        return false;
    }

    self.killed_boss_list.push({
        boss_id: self.curr_boss_id,
        killer_id: role_id,
        killer_name: role_name
    });

    return true;
};

/**
 * 重置联盟BOSS
 * @return {[type]} [description]
 */
unionBossSchema.methods.resetBoss = function () {
    var first_boss_conf = unionBossConf.getFirstBossConf();
    this.damage_list = [];
    this.killed_boss_list = [];
    this.lock_role_id = 0;
    this.lock_end_tm = 0;
    this.curr_boss_id = first_boss_conf.id;
    this.curr_boss_hp = first_boss_conf.hp;
};

mongoose.model('UnionBoss', unionBossSchema);
